package com.example.demo.Controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import com.example.demo.POJO.tEmployee1;
import com.example.demo.Service.Employee1Service;
import com.example.demo.commen.OperationLogAnnotation;
import com.example.demo.commen.ResultUtil;
import com.example.demo.commen.WebSocketServer;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
public class Employee1Controller {

	@Autowired
	private Employee1Service emp;
	
	@Autowired
	private WebSocketServer webSocketServer;
	
//	@Autowired
//	private AsyncSendEmail
	
	
	//查询所有
	@OperationLogAnnotation(operModul = "用户模块-用户列表",operType = "查询",operDesc = "查询所有用户")
	@GetMapping("/findAll")
	public ResultUtil findAll(HttpServletRequest request){
		log.info("执行查询所有员工操作！");
		request.setAttribute("userCode", "admin");
		try {
			webSocketServer.sendInfo(emp.findAll().toString());
			//webSocketServer.sendMessage("you maybe a good guy");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ResultUtil.success(emp.findAll());
	}
	
	//查询单个
	@PostMapping("/findById/{eid}")
	public tEmployee1 findById(@PathVariable("eid") Integer eid){
		System.out.println("执行查询单个员工操作！员工编号为"+eid);
		return emp.findById(eid);
	}
	
	//密码比较
	@PostMapping("/equalPassword/{eid}/{password}")
	public String equalPassword(@PathVariable("eid") Integer eid,@PathVariable("password") String password) {		
		return emp.equalPassword(eid,password);	
	}
	
	//添加
	@PostMapping("/add")
	public int add(@RequestBody tEmployee1 employee1) {
		return emp.addEmployee(employee1);
	}
	
	//实现邮件注册接口
	
	
	//修改
	@PostMapping("/edit")
	public int update(@RequestBody tEmployee1 employee1) {
		System.out.println("执行修改单个员工操作！员工编号为"+employee1.getEid());
		return emp.updateEmployee(employee1);
	}
	
	//删除
	@PostMapping("/delete/{eid}")
	public int delete(@PathVariable("eid") Integer eid) {
		System.out.println("执行删除员工的操作！");
		return emp.deleteEmployee(eid);
	}
	
}
